package com.jungle.sp.daily.controller;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeiboRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


/**
 * @author 86189
 */
@Slf4j
@RestController
@RequestMapping("/oauth")
public class RestAuthController {
    // public static final String CLIENT_ID = "58203416607-cpm19l98658u48lhjiik9bh4vl67hk83.apps.googleusercontent.com";
    // private static final String CLIENT_SECRET = "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";
    // private static final String REDIRECT_URI = "http://www.rp.ersan.click";


    public static final String CLIENT_ID = "1234340891";
    private static final String CLIENT_SECRET = "446d2b02a6b051a5184c7b9809a9a0c2";
    private static final String REDIRECT_URI = "http://www.rp.ersan.click/oauth2.0/weibo/success";

    @RequestMapping("/render")
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/callback")
    public Object login(AuthCallback callback) {
        log.info("回调请求参数==>{}", JSON.toJSON(callback));
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(callback);
    }

    private AuthRequest getAuthRequest() {
        return new AuthWeiboRequest(AuthConfig.builder()
                .clientId(CLIENT_ID)
                .clientSecret(CLIENT_SECRET)
                .redirectUri(REDIRECT_URI)
                // 针对国外平台配置代理
                // .httpConfig(HttpConfig.builder()
                //         .timeout(15000)
                //         // host 和 port 请修改为开发环境的参数
                //         .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809)))
                //         .build())
                .build());
    }
}
